package com.tj.test.sys.controller;

import com.tj.test.sys.po.PST;
import com.tj.test.sys.po.Teacher;
import com.tj.test.sys.service.impl.TeacherService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/teacher")
@Api(tags = "teacher")
public class TeacherController {
    @Autowired
    private TeacherService service;

    @ApiOperation("查询所有教师信息")
    @RequestMapping(value = "/selectAllTeacher",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectAllTeacher(){
        Map<String,List<Teacher>> map=new HashMap<>();
        List<Teacher> list=service.selectAllTeacher();
        if (!list.isEmpty()){
            map.put("AllTeacher",list);
            return Result.success("查询成功",map);
        }
        return Result.error("查询失败",false);
    }



    @ApiOperation("删除教师信息")
    @RequestMapping(value = "/deleteTeacher",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage deleteTeacher(PST teacher){
        int temp=0;
        if (teacher!=null){
            if (teacher.getPersonid()!=0){
                temp=service.deleteTeacher(teacher);
                if (temp>0) {return Result.success("删除教师信息成功",temp);}
                else {return Result.error("无该教师",temp);}
            }
        }
        return Result.error("删除教师信息失败",temp);
    }

    @ApiOperation("修改教师信息")
    @RequestMapping(value = "/updateTeacher",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage updateTeacher(PST teacher){
        int temp=0;
        if (teacher!=null){
            if (teacher.getPersonid()!=0){
                temp=service.updateTeacher(teacher);
                if (temp>0) {return Result.success("更新教师信息成功",temp);}
                else {return Result.error("无该教师信息",temp);}
            }
        }
        return Result.error("更新教师信息失败",temp);
    }

}
